{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Deep Learning Models -- A collection of various deep learning architectures, models, and tips for TensorFlow and PyTorch in Jupyter Notebooks.\n",
    "- Author: Sebastian Raschka\n",
    "- GitHub Repository: https://github.com/rasbt/deeplearning-models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sebastian Raschka \n",
      "\n",
      "CPython 3.6.8\n",
      "IPython 7.2.0\n",
      "\n",
      "tensorflow 1.12.0\n"
     ]
    }
   ],
   "source": [
    "%load_ext watermark\n",
    "%watermark -a 'Sebastian Raschka' -v -p tensorflow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model Zoo -- Convolutional Neural Network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Low-level Implementation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-2-70b056af7052>:10: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From /home/raschka/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From /home/raschka/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting ./train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From /home/raschka/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting ./train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From /home/raschka/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting ./t10k-images-idx3-ubyte.gz\n",
      "Extracting ./t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From /home/raschka/miniconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from functools import reduce\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "\n",
    "##########################\n",
    "### DATASET\n",
    "##########################\n",
    "\n",
    "mnist = input_data.read_data_sets(\"./\", one_hot=True)\n",
    "\n",
    "\n",
    "##########################\n",
    "### SETTINGS\n",
    "##########################\n",
    "\n",
    "# Hyperparameters\n",
    "learning_rate = 0.1\n",
    "dropout_keep_proba = 0.5\n",
    "epochs = 3\n",
    "batch_size = 32\n",
    "\n",
    "# Architecture\n",
    "input_size = 784\n",
    "image_width, image_height = 28, 28\n",
    "n_classes = 10\n",
    "\n",
    "# Other\n",
    "print_interval = 500\n",
    "random_seed = 123\n",
    "\n",
    "\n",
    "##########################\n",
    "### WRAPPER FUNCTIONS\n",
    "##########################\n",
    "\n",
    "def conv2d(input_tensor, output_channels,\n",
    "           kernel_size=(5, 5), strides=(1, 1, 1, 1),\n",
    "           padding='SAME', activation=None, seed=None,\n",
    "           name='conv2d'):\n",
    "\n",
    "    with tf.name_scope(name):\n",
    "        input_channels = input_tensor.get_shape().as_list()[-1]\n",
    "        weights_shape = (kernel_size[0], kernel_size[1],\n",
    "                         input_channels, output_channels)\n",
    "\n",
    "        weights = tf.Variable(tf.truncated_normal(shape=weights_shape,\n",
    "                                                  mean=0.0,\n",
    "                                                  stddev=0.01,\n",
    "                                                  dtype=tf.float32,\n",
    "                                                  seed=seed),\n",
    "                              name='weights')\n",
    "        biases = tf.Variable(tf.zeros(shape=(output_channels,)), name='biases')\n",
    "        conv = tf.nn.conv2d(input=input_tensor,\n",
    "                            filter=weights,\n",
    "                            strides=strides,\n",
    "                            padding=padding)\n",
    "\n",
    "        act = conv + biases\n",
    "        if activation is not None:\n",
    "            act = activation(conv + biases)\n",
    "        return act\n",
    "\n",
    "\n",
    "def fully_connected(input_tensor, output_nodes,\n",
    "                    activation=None, seed=None,\n",
    "                    name='fully_connected'):\n",
    "\n",
    "    with tf.name_scope(name):\n",
    "        input_nodes = input_tensor.get_shape().as_list()[1]\n",
    "        weights = tf.Variable(tf.truncated_normal(shape=(input_nodes,\n",
    "                                                         output_nodes),\n",
    "                                                  mean=0.0,\n",
    "                                                  stddev=0.01,\n",
    "                                                  dtype=tf.float32,\n",
    "                                                  seed=seed),\n",
    "                              name='weights')\n",
    "        biases = tf.Variable(tf.zeros(shape=[output_nodes]), name='biases')\n",
    "\n",
    "        act = tf.matmul(input_tensor, weights) + biases\n",
    "        if activation is not None:\n",
    "            act = activation(act)\n",
    "        return act\n",
    "\n",
    "    \n",
    "##########################\n",
    "### GRAPH DEFINITION\n",
    "##########################\n",
    "\n",
    "g = tf.Graph()\n",
    "with g.as_default():\n",
    "    \n",
    "    tf.set_random_seed(random_seed)\n",
    "\n",
    "    # Input data\n",
    "    tf_x = tf.placeholder(tf.float32, [None, input_size, 1], name='inputs')\n",
    "    tf_y = tf.placeholder(tf.float32, [None, n_classes], name='targets')\n",
    "    \n",
    "    keep_proba = tf.placeholder(tf.float32, shape=None, name='keep_proba')\n",
    "\n",
    "    # Convolutional Neural Network:\n",
    "    # 2 convolutional layers with maxpool and ReLU activation\n",
    "    input_layer = tf.reshape(tf_x, shape=[-1, image_width, image_height, 1])\n",
    "    \n",
    "    conv1 = conv2d(input_tensor=input_layer,\n",
    "                   output_channels=8,\n",
    "                   kernel_size=(3, 3),\n",
    "                   strides=(1, 1, 1, 1),\n",
    "                   activation=tf.nn.relu,\n",
    "                   name='conv1')\n",
    "                              \n",
    "    pool1 = tf.nn.max_pool(conv1,\n",
    "                           ksize=(1, 2, 2, 1), \n",
    "                           strides=(1, 2, 2, 1),\n",
    "                           padding='SAME',\n",
    "                           name='maxpool1')\n",
    "    \n",
    "    conv2 = conv2d(input_tensor=pool1,\n",
    "                   output_channels=16,\n",
    "                   kernel_size=(3, 3),\n",
    "                   strides=(1, 1, 1, 1),\n",
    "                   activation=tf.nn.relu,\n",
    "                   name='conv2')\n",
    "    \n",
    "    pool2 = tf.nn.max_pool(conv2,\n",
    "                           ksize=(1, 2, 2, 1), \n",
    "                           strides=(1, 2, 2, 1),\n",
    "                           padding='SAME',\n",
    "                           name='maxpool2')\n",
    "    \n",
    "    dims = pool2.get_shape().as_list()[1:]\n",
    "    dims = reduce(lambda x, y: x * y, dims, 1)\n",
    "    flat = tf.reshape(pool2, shape=(-1, dims))\n",
    "    \n",
    "    out_layer = fully_connected(flat, n_classes, activation=None, \n",
    "                                name='logits')\n",
    "\n",
    "    # Loss and optimizer\n",
    "    loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=out_layer, labels=tf_y)\n",
    "    cost = tf.reduce_mean(loss, name='cost')\n",
    "    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n",
    "    train = optimizer.minimize(cost, name='train')\n",
    "\n",
    "    # Prediction\n",
    "    correct_prediction = tf.equal(tf.argmax(tf_y, 1), \n",
    "                                  tf.argmax(out_layer, 1), \n",
    "                         name='correct_prediction')\n",
    "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, \n",
    "                                      tf.float32), \n",
    "                              name='accuracy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Minibatch: 001 | Cost: 2.303\n",
      "Minibatch: 501 | Cost: 0.225\n",
      "Minibatch: 1001 | Cost: 0.106\n",
      "Minibatch: 1501 | Cost: 0.039\n",
      "Epoch: 001 | AvgCost: 0.530 | Train/Valid ACC: 0.966/0.964\n",
      "Minibatch: 001 | Cost: 0.051\n",
      "Minibatch: 501 | Cost: 0.035\n",
      "Minibatch: 1001 | Cost: 0.043\n",
      "Minibatch: 1501 | Cost: 0.058\n",
      "Epoch: 002 | AvgCost: 0.102 | Train/Valid ACC: 0.967/0.968\n",
      "Minibatch: 001 | Cost: 0.019\n",
      "Minibatch: 501 | Cost: 0.132\n",
      "Minibatch: 1001 | Cost: 0.064\n",
      "Minibatch: 1501 | Cost: 0.011\n",
      "Epoch: 003 | AvgCost: 0.076 | Train/Valid ACC: 0.978/0.978\n",
      "Test ACC: 0.980\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "##########################\n",
    "### TRAINING & EVALUATION\n",
    "##########################\n",
    "\n",
    "with tf.Session(graph=g) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "\n",
    "    np.random.seed(random_seed) # random seed for mnist iterator\n",
    "    for epoch in range(1, epochs + 1):\n",
    "        avg_cost = 0.\n",
    "        total_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "        for i in range(total_batch):\n",
    "            batch_x, batch_y = mnist.train.next_batch(batch_size)\n",
    "            batch_x = batch_x[:, :, None] # add \"missing\" color channel\n",
    "            \n",
    "            _, c = sess.run(['train', 'cost:0'], \n",
    "                            feed_dict={'inputs:0': batch_x,\n",
    "                                       'targets:0': batch_y,\n",
    "                                       'keep_proba:0': dropout_keep_proba})\n",
    "            avg_cost += c\n",
    "            if not i % print_interval:\n",
    "                print(\"Minibatch: %03d | Cost: %.3f\" % (i + 1, c))\n",
    "        \n",
    "        train_acc = sess.run('accuracy:0', \n",
    "                             feed_dict={'inputs:0': mnist.train.images[:, :, None],\n",
    "                                        'targets:0': mnist.train.labels,\n",
    "                                        'keep_proba:0': 1.0})\n",
    "        valid_acc = sess.run('accuracy:0', \n",
    "                             feed_dict={'inputs:0': mnist.validation.images[:, :, None],\n",
    "                                        'targets:0': mnist.validation.labels,\n",
    "                                        'keep_proba:0': 1.0})\n",
    "        \n",
    "        print(\"Epoch: %03d | AvgCost: %.3f\" % (epoch, avg_cost / (i + 1)), end=\"\")\n",
    "        print(\" | Train/Valid ACC: %.3f/%.3f\" % (train_acc, valid_acc))\n",
    "        \n",
    "    test_acc = sess.run('accuracy:0', \n",
    "                        feed_dict={'inputs:0': mnist.test.images[:, :, None],\n",
    "                                   'targets:0': mnist.test.labels,\n",
    "                                   'keep_proba:0': 1.0})\n",
    "        \n",
    "    print('Test ACC: %.3f' % test_acc)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
